package com.zhihao.takeaway.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhihao.takeaway.common.utils.DateTool;
import com.zhihao.takeaway.common.utils.EasyUITable;
import com.zhihao.takeaway.entity.Chat;
import com.zhihao.takeaway.mapper.ChatMapper;
import com.zhihao.takeaway.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.UUID;

@Service
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat>
        implements ChatService {

    @Autowired
    private ChatMapper chatMapper;

    @Override
    public EasyUITable selectEncord(String tableName, String fromAccount, String toAccount) {

        List<Chat> list = chatMapper.selectEncord(tableName,fromAccount,toAccount);
        return new EasyUITable(list);
    }

    @Override
    @Transactional
    public void saveEncord(String fromAccount, String toAccount, String message) {
        String date = DateTool.getCurrTime();
        try {
            String uuid1 = UUID.randomUUID().toString();
            chatMapper.saveEncord("record" + fromAccount, fromAccount, toAccount, message, date, uuid1);
            String uuid2 = UUID.randomUUID().toString();
            chatMapper.saveEncord("record" + toAccount, fromAccount, toAccount, message, date, uuid2);
        }catch(Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
    }
}
